package com.example.module_parking

import android.text.Editable
import android.text.TextWatcher
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.ImageView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.utils.ToastUtils
import com.example.module_parking.adapter.ParkingAdapter
import com.example.module_parking.databinding.ActivityParkingBinding
import com.example.module_parking.model.ParkingEntity
import com.example.module_parking.ui.ParkingViewModel
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
@Route(path = "/module_parking/ParkingActivity")
class ParkingActivity : BaseActivity<ParkingViewModel, ActivityParkingBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_parking
    private val mViewModel: ParkingViewModel by viewModels()
    lateinit var pkAdapter: ParkingAdapter
    private var back: ImageView? = null
    var list: ArrayList<ParkingEntity> = arrayListOf()
    override fun initData() {
        pkAdapter = ParkingAdapter()
        mBinding.rv.adapter = pkAdapter
        mBinding.rv.layoutManager = LinearLayoutManager(this)
        //根据车位类型获取车位信息
        mViewModel.getParkingList("标准车位")

        // 加载原始数据
        loadOriginalData();

        // 设置搜索监听
        setupSearchFunctionality();

        back=mBinding.topBar.findViewById(R.id.back)
        back!!.setOnClickListener {
            finish()
        }
    }

    private fun loadOriginalData() {
        mViewModel.parkingListLiveData.observe(this) {
            if (it.isNullOrEmpty()) {
                ToastUtils.showLong(this, "没有数据") // 显示空状态
            } else {
                pkAdapter.addParkingList(it)
            }
        }
    }

    private fun setupSearchFunctionality() {
        // 1. 回车键搜索
        mBinding.search.setOnEditorActionListener { v, actionId, event ->
            if (actionId === EditorInfo.IME_ACTION_SEARCH ||
                (event != null && event.getKeyCode() === KeyEvent.KEYCODE_ENTER)
            ) {
                performSearch()
                return@setOnEditorActionListener true
            }
            false
        }

        // 2. 文本变化监听，当清空时恢复原始列表
        mBinding.search.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (s.toString().trim { it <= ' ' }.isEmpty()) {
                    // 搜索框内容被清空，恢复原始列表
                    mViewModel.getParkingList("标准车位")
                }
            }

            override fun afterTextChanged(s: Editable) {}
        })

    }

    private fun performSearch() {
        //3156
        mViewModel.searchParking(mBinding.search.text.toString().toInt())

        mViewModel.searchParkingLiveData.observe(this) {
            if (it == null) {
                ToastUtils.showLong(this, "没有数据") // 显示空状态
            } else {
                pkAdapter.addSearchParking(it)
            }

        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getParkingList("标准车位")
    }

}